package com.bl.service.order;

import java.util.Random;

import com.bean.Customer;
import com.bean.Order;
import com.bl.cash.DataContainer;
import com.bl.service.BeanFactory;
import com.constants.CustomerConstants;
import com.constants.ProductConstants;
import com.dao.factory.DaoFactory.FactoryType;

/**
 * @author ${user}
 */
public class OrderFactory implements BeanFactory<Order>{

    private final FactoryType defaultFactoryType;

	public OrderFactory(FactoryType aDefaultFactoryType) {
		defaultFactoryType = aDefaultFactoryType;
	}


	@Override
    public Order createBean() {
        Order order = null;
        switch (defaultFactoryType) {
			case JDBC:
				order = new com.dao.jdbc.beans.Order();
				break;

			default:
				order = new Order();
				break;
		}
        return populateOrder(order);
    }


    @Override
    public Order updateBean(Order aBean) {
        return populateOrder(aBean);
    }

    private Order populateOrder(Order aOrder) {
        Random random = new Random();
        int randomCustomer = random.nextInt(CustomerConstants.getNumberOfCustomers());
        int randomProduct = random.nextInt(ProductConstants.getNumberOfProducts());
        Customer customer = DataContainer.getCustomers().get(randomCustomer);
        aOrder.setCustomer(customer);
        aOrder.setProduct(DataContainer.getProducts().get(randomProduct));
        return aOrder;
    }
}
